package com.client;

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JScrollPane;

import com.client.gui.DoodleGuiTestMain;
import com.client.gui.DoodleTextArea;
import com.client.listeners.DoodleListener;
import com.packages.ClientPackageHandler;
import com.packages.ServerPackageHandler;

@SuppressWarnings("serial")
public class ClientMain extends Applet implements Runnable{
	public static final boolean	AUTO_JOIN	= true;
	private int					width, height;

	private CommandSender		cs;
	private CommandReceiver		cr;
	private MessageHandler		mh;
	private Monitor				m;
	private DrawArea			da;

	public void init()
	{

		Socket s = null;
		try
		{
			s = new Socket("localhost", 30000);
		} catch (UnknownHostException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}

		width = 1920;
		height = 2500;
		DoodleTextArea dta = new DoodleTextArea();
		ClientPackageHandler cph = new ClientPackageHandler();
		ServerPackageHandler sph = new ServerPackageHandler();
		JScrollPane scrollDta = new JScrollPane(dta);
		scrollDta.setPreferredSize(new Dimension(1400, 800));

		GUIMonitor guim = new GUIMonitor();

		m = new Monitor(width, height, this, dta);
		da = new DrawArea(m, height, null);
		cs = new CommandSender(s, m, cph, guim);
		DoodleListener dl = new DoodleListener(cs, da);
		cr = new CommandReceiver(s, m);
		mh = new MessageHandler(m, guim, sph, dl);
		cs.start();
		cr.start();
		mh.start();

		da.setPreferredSize(new Dimension(800, 500));
		da.addMouseMotionListener(dl);
		da.addMouseListener(dl);
		da.addKeyListener(dl);
		da.addMouseWheelListener(dl);

		setFocusable(true);
		this.addKeyListener(dl);
		setSize(1400, 800);
		add(new DoodleGuiTestMain(guim, da, dl));
	}

	public void start()
	{

	}

	public void paint(Graphics g)
	{
		super.paint(g);
		requestFocus();
		requestFocusInWindow();
		
	}

	public void stop()
	{

	}

	@Override
	public void run()
	{

	}
}
